package cn.stronglink.assetmanage.business.repair.assetlist;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Scaner.InfraredScanerFactory;
import Scaner.scan.IScanerListener;
import Scaner.scan.TimerInfraraedScaner;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.assetlist.AssetListActivity;
import cn.stronglink.assetmanage.business.repair.detail.RepairDetailActivity;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.MessageEvent;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.utils.dao.RepairDataUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class RepairDetailFragment extends Fragment {
    private String orderNo, location;
    private Unbinder mUnbinder;
    private List<RepailDetail> mAssetList = new ArrayList<>();
    @BindView(R.id.rv_repair_asset_list)
    RecyclerView rvRepairAssetList;
    @BindView(R.id.cb_check)
    CheckBox cb_check;
    @BindView(R.id.tv_picture)
    TextView tv_picture;
    private RepairDetailAdapter mAdapter;
    private RepairDetailListActivity mActivity;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (RepairDetailListActivity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repair_detail, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cb_check.setVisibility(View.GONE);
        tv_picture.setVisibility(View.GONE);
        mAdapter = new RepairDetailAdapter(mActivity);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity,
                LinearLayoutManager.VERTICAL, false);
        rvRepairAssetList.setLayoutManager(layoutManager);
        rvRepairAssetList.setAdapter(mAdapter);
        setOrderInfo(RepairDetailListActivity.orderNo, mActivity.getString(R.string.all_location));
    }


    public void setOrderInfo(final String orderNo, final String location) {
        this.orderNo = orderNo;
        this.location = location;
        mAssetList = RepairDataUtil.getRepairedAssetByOrderNo(orderNo, location);
        if (mActivity != null) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mAdapter != null) {
                        mAdapter.notifyDataChanged(mAssetList);
                    }
                }
            });
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateData(MessageEvent messageEvent) {
        if (messageEvent != null && Constants.FIFTLER_REPAIR_DATA.equals(messageEvent.getType())) {
            String[] dataArr = (String[]) messageEvent.getMessage();
            String orderNo = dataArr[0];
            String roomName = dataArr[1];
            setOrderInfo(orderNo, roomName);
        }
    }

}
